package com.itheima.web;

import com.itheima.bean.ShoppingCart;
import com.itheima.common.R;
import com.itheima.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RequestMapping("/shoppingCart")
@RestController
public class ShoppingCartController {

    @Resource
    private ShoppingCartService shoppingCartService;

    @PostMapping("/add")
    public R add(@RequestBody ShoppingCart shoppingCart) {


        ShoppingCart add = shoppingCartService.add(shoppingCart);


        return R.success(add);


    }

    @PostMapping("/sub")
    public R sub(@RequestBody ShoppingCart shoppingCart) {


        ShoppingCart sub = shoppingCartService.sub(shoppingCart);


        return R.success(sub);


    }

    @GetMapping("/list")
    public R findList() {
        List<ShoppingCart> list = shoppingCartService.findList();
        return R.success(list);
    }

    @DeleteMapping("/clean")
    public R clean() {
        int row = shoppingCartService.clean();
        if (row > 0) {
            return R.success("清空购物车成功！");
        } else {
            return R.error("清空购物车失败！");
        }
    }
}
